package com.one.consumer.reserve.presenter;

import android.content.Context;

import com.one.common.common.user.modle.UserInfoModel;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.http.callback.ObserverOnNextListener;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.MyToast;
import com.one.common.utils.Toaster;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.base.BaseActivity;
import com.one.consumer.reserve.model.ReserveModel;
import com.one.consumer.reserve.model.bean.TimeBean;
import com.one.consumer.reserve.model.extra.AreaExtra;
import com.one.common.common.order.extra.ReserveExtra;
import com.one.consumer.reserve.ui.view.AddServiceView;
import com.one.consumer.user.model.UserModel;
import com.one.consumer.user.model.item.AddressItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/28 3:20 PM
 * description ：一键报修
 **/
public class AddServicePresenter extends BaseApiPresenter<AddServiceView, ReserveModel> {
    public AddServicePresenter(AddServiceView mView, Context mContext) {
        super(mView, mContext, new ReserveModel((BaseActivity) mContext));
    }


    /**
     * 获取服务时间
     */
    public void getTimeList() {
        mModel.getTimeList("", new ObserverOnResultListener<ArrayList<TimeBean>>() {
            @Override
            public void onResult(ArrayList<TimeBean> timeBeans) {
                if (mView != null) {
                    ArrayList<TimeBean.TimeBeansBean> beans = new ArrayList<>();
                    for (TimeBean bean : timeBeans) {
                        beans.addAll(bean.getTimeBeans());
                    }
                    mView.setTimeData(beans);
                }
            }
        });
    }

    /**
     * 得到用户的默认地址
     */
    public void getDefaultAddress() {
        new UserModel(mActivity).getDefaultAddress(new ObserverOnResultListener<AddressItem>() {
            @Override
            public void onResult(AddressItem item) {
                if (mView != null) {
                    mView.setAddressData(item);
                }
            }
        });
    }

    /**
     * 提交报修
     */
    Map<String, String> params = new HashMap<>();

    public void submit(ArrayList<String> paths, AddressItem item,
                       String maintail_remark, AreaExtra areaExtra, TimeBean.TimeBeansBean timeBeansBean) {

        if (item == null) {
            Toaster.showLongToast("请选择地址");
            return;
        } else {
            params.put("address_id", item.getAddress_id() + "");
            params.put("address_latitude", item.getAddress_latitude() + "");
            params.put("address_longitude", item.getAddress_longitude() + "");
        }
        if (areaExtra == null) {
            Toaster.showLongToast("请选择区域");
            return;
        } else {
            params.put("parts_ids", ListUtils.join(areaExtra.getIds()));
            params.put("parts_names", ListUtils.join(areaExtra.getNames()));
        }
        // 描述非必填
//        if(StringUtils.isBlank(maintail_remark)){
//            Toaster.showLongToast("请输入维修描述");
//            return;
//        } else {
//            params.put("maintail_remark", maintail_remark);
//        }

        if (timeBeansBean == null) {
            Toaster.showLongToast("请选择服务时间");
            return;
        } else {
            params.put("service_start_time", timeBeansBean.getService_time() + " " + timeBeansBean.getStart_time());
            params.put("service_end_time", timeBeansBean.getService_time() + " " + timeBeansBean.getEnd_time());
        }

        if(mView==null)return;
        mView.startUpload();
        if (ListUtils.isNotEmpty(paths)) {
            uploadImgs(paths);
        } else {
            // 图片非必填
            order();
        }


    }


    /**
     * 上传图片
     */
    public void uploadImgs(ArrayList<String> paths) {
        new UserInfoModel(mActivity).uploadImgs(paths, new ObserverOnResultListener<ArrayList<String>>() {
            @Override
            public void onResult(ArrayList<String> s) {
                if (ListUtils.isNotEmpty(s)) {
                    params.put("maintail_imgs", ListUtils.join(s));
                    order();
                }
            }
        });
    }

    /**
     * 报修下单
     */
    private void order() {
        mModel.order(params, new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                if(mView == null)return;
                mView.finishUpload();
                RouterManager.getInstance().goAndFinish(mActivity, RouterPath.RESERVE_SUCCESS,
                        new ReserveExtra(ReserveExtra.STYLE_SR));
            }

            @Override
            public void onError(String errorCode, String msg) {
                if(mView == null)return;
                mView.finishUpload();
                Toaster.showLongToast(msg, MyToast.FAIL);
            }
        });
    }

}
